package com.lp.company.controller;

import com.lp.common.entity.Result;
import com.lp.company.response.DeptListResult;
import com.lp.company.service.CompanyService;
import com.lp.company.service.DepartmentService;
import com.lp.domain.company.Company;
import com.lp.domain.company.Department;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lipengak
 * @Description 1. 解决跨域
 * 2. 什么restController
 * 3. 设置父路径
 * @Date 2020/1/31 16:50
 * @Copyright 2019-2020
 */
@CrossOrigin
@RestController
@RequestMapping("/department")
public class DepartmentController {


    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private CompanyService companyService;

    /**
     * CRUD
     */
    @PostMapping("/")
    public Result save(@RequestBody Department department) {
        //设置企业ID
        department.setCompanyId("1");
        departmentService.add(department);
        return Result.SUCCESS();
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable String id) {
        return Result.SUCCESS(departmentService.findById(id));
    }

    @PutMapping("/")
    public Result update(@RequestBody Department department) {
        //设置企业ID
        department.setCompanyId("1");
        departmentService.update(department);
        return Result.SUCCESS();
    }
    @GetMapping("/")
    public Result findAll() {
        String companyId = "1";
        Company company = companyService.findById(companyId);
        List<Department> depts = departmentService.findAll(companyId);
        return Result.SUCCESS(new DeptListResult(company, depts));
    }

    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable String id) {
        departmentService.deleteById(id);
        return Result.SUCCESS();
    }
}
